package com.artisan.web.action;

import com.artisan.common.pojo.PageBean;
import com.artisan.commonweb.result.ActionResult;
import com.artisan.medium.admin.service.WorkerService;
import com.artisan.medium.admin.pojo.WorkerForm;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by szw on 2017/7/4.
 */
@Controller
@RequestMapping("/admin/worker/")
public class WorkerController {

	private static final Logger log = LogManager.getLogger(WorkerController.class);

	@Autowired
	private WorkerService workerService;

	@RequestMapping("init_addWorker")
	public ModelAndView init_addWorker(HttpServletRequest request, HttpServletResponse response) {
		return new ModelAndView("worker/add_worker");
	}

	@RequestMapping(value = "addWorker", method = RequestMethod.POST)
	public ModelAndView createWorker(HttpServletRequest request, HttpServletResponse response, WorkerForm workerForm, MultipartFile file) {
		try {
			String fileName = file.getOriginalFilename();
			if (file != null && StringUtils.isNotBlank(fileName)) {
				String path = request.getSession().getServletContext().getRealPath("/upload/worker/");
				File saveFile = new File(path);
				if (!saveFile.exists()) {
					saveFile.mkdirs();
				}
				//文件命名
				fileName = System.currentTimeMillis() + fileName.substring(fileName.lastIndexOf("."));
				File newFile = new File(path, fileName);
				file.transferTo(newFile);
				workerForm.setLogo("/upload/worker/" + fileName);
				workerService.addWorker(workerForm);
			}
		} catch (Exception e) {
			log.error("Add worker failed.", e);
			e.printStackTrace();
		}
		return new ModelAndView("redirect:/admin/worker/worker_list_page.do");
	}

	/**
	 * 工人列表页
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("worker_list_page")
	public ModelAndView initWorkerList(HttpServletRequest request, HttpServletResponse response) {

		return new ModelAndView("worker/worker_list");
	}

	/**
	 *
	 * @param busId
	 * @param proId
	 * @param cityId
	 * @param areaId
	 * @param ide
	 * @param phone
	 * @param offset
	 * @param limit
	 * @return
	 */
	@ResponseBody
	@RequestMapping("queryWorkers")
	public Object queryWorkerList(Integer busId, String proId, String cityId, String areaId, String ide, String phone,
	                              @RequestParam(defaultValue = "0") Integer offset, @RequestParam(defaultValue = "10") Integer limit) {
		PageBean pageBean = workerService.queryWorkers(busId, proId, cityId, areaId, ide, phone, offset/limit + 1, limit);

		return pageBean;
	}

	@RequestMapping("worker_info")
	public ModelAndView queryWorkerInfo(HttpServletRequest request, HttpServletResponse response, Integer workerId) {

		Map<String, Object> result = workerService.queryWorkerInfo(workerId);
		return new ModelAndView("worker/worker_info", result);
	}

	/**
	 * 修改工人信息
	 * @param request
	 * @param response
	 * @param workerId
	 * @return
	 */
	@RequestMapping("edit_worker")
	public ModelAndView editWorkerInfo(HttpServletRequest request, HttpServletResponse response, Integer workerId) {

		Map<String, Object> result = new HashMap<>();
		result = workerService.editWorker(workerId);
		return new ModelAndView("worker/add_worker", result);
	}

	/**
	 * 删除工人师傅信息
	 * @param workerId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("del_worker")
	public ActionResult delWorker(Integer workerId) {
		ActionResult actionResult = new ActionResult();
		int i = workerService.delWorker(workerId);
		if(i == 1){
			actionResult.setCode(200);
		} else if (i == 2) {
			actionResult.setData(workerId);
			actionResult.setCode(300);
		} else{
			actionResult.setCode(500);
		}
		return  actionResult;
	}

	@RequestMapping("assess_page")
	public ModelAndView workerAssessPage(Integer workerId) {
		Map<String, Object> result = new HashMap<>();
		result.put("workerId", workerId);
		return new ModelAndView("worker/worker_assess", result);
	}

	@ResponseBody
	@RequestMapping("worker_assess")
	public PageBean queryWorkerAssess(Integer workerId, @RequestParam(defaultValue = "1") Integer offset,
	                                  @RequestParam(defaultValue = "10") Integer limit) {

		PageBean pageBean = workerService.queryWorkerAssess(workerId, offset/limit + 1, limit);
		return pageBean;
	}
}
